<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="https://baidu.com" method="post" id="form">
        用户名: <input type="text" name="user" id="">
        <hr>
        <input type="checkbox" name="copyright" id="">接受协议
        <hr>
        <button>提交</button>

    </form>
    <script>
        function query(el) {
            return document.querySelector(el);
        }

        query("#form").addEventListener("submit", function (event) {
            let user = query("[name='user']").value.trim();
            let copyright = query("[name='copyright']").checked;
            console.log(user);
            console.log(copyright);

            if (!user || copyright === false) {
                event.preventDefault();
                alert("请接受协议并添加姓名");
            }

        });
    </script>
</body>

</html>